package cn.com.ddapp.ddm.ui.mine;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.zhy.autolayout.AutoLinearLayout;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.adapter.ProviceListAdapter1;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.ATMAddress;
import cn.com.ddapp.ddm.bean.CourierBean;
import cn.com.ddapp.ddm.bean.ShopNumsBean;
import cn.com.ddapp.ddm.bean.mine.AddAddress;
import cn.com.ddapp.ddm.bean.mine.Addresses;
import cn.com.ddapp.ddm.bean.mine.MyAddress;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.util.FileUtil;
import cn.com.ddapp.ddm.util.NetUtils;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.StringUtils;
import cn.com.ddapp.ddm.util.TDevice;
import cn.com.ddapp.ddm.widget.TipsToast;
import okhttp3.Call;

import static cn.com.ddapp.ddm.R.id.edt_address;


public class AddressEdtActivity extends BaseActivity {

//	private ProviceListAdapter adapter1;
//	private CityListAdapter adapter2;
//	private DistrictListAdapter adapter3;
    private ProviceListAdapter1 adapter1;
    private ProviceListAdapter1 adapter2;
    private ProviceListAdapter1 adapter3;
    private ProviceListAdapter1 adapter4;
    private ProviceListAdapter1 adapter5;
	private PopupWindow window1;
	private PopupWindow window2;
	private PopupWindow window3;
	private PopupWindow window4;
	private PopupWindow window5;

	@BindView(R.id.tv_province)
	public TextView province;
	@BindView(R.id.tv_city)
	public TextView city;
	@BindView(R.id.tv_district)
	public TextView district;
    @BindView(R.id.tv_zhen)
    TextView zhen;
    @BindView(R.id.tv_cun)
    TextView cun;
	@BindView(R.id.cb_select)
	public CheckBox cb_select;

	@BindView(R.id.edt_name)
	public EditText nameEdt;
	@BindView(R.id.edt_tel)
	public EditText telEdt;

    @BindView(R.id.shopSS)
    public TextView shopSS;//搜索网点
    @BindView(R.id.shopSSRL)
    public RelativeLayout shopSSRL;//搜索网点
    @BindView(R.id.wddzrl)
    public RelativeLayout wddzrl;//搜索网点
    @BindView(R.id.shopCode)
    public EditText shopCode;
    @BindView(R.id.addressLL)
    public LinearLayout addressLL;
    @BindView(R.id.wddzet)
    public EditText wddzet;


	public ListView lv;
	boolean isChoose1 = false;
	boolean isChoose2 = false;
	boolean isChoose3 = false;
	boolean isChoose4 = false;
	boolean isChoose5 = false;
	private int acceptId;
	private String userId;
    int refId;
    int parentIdCity=0;
    int provinceId;
    int cityId;
    int districtId;
    int townId;
    int countryId;
    List<MyAddress.ListBean> listBean ;
    //省
    private List<MyAddress.ListBean> mProvinceDatas ;
    //市
    private List<MyAddress.ListBean> mCitisDatas ;
    //区
    private List<MyAddress.ListBean> mDistrictDatas ;
    //镇
    private List<MyAddress.ListBean> mTownDatas ;
    //村
    private List<MyAddress.ListBean> mCountryDatas ;

    @BindView(R.id.select_province)
    public AutoLinearLayout select_province;
    @BindView(R.id.select_city)
    public AutoLinearLayout select_city;
    @BindView(R.id.select_district)
    public AutoLinearLayout select_district;
    @BindView(R.id.select_zhen)
    public AutoLinearLayout select_zhen;
    @BindView(R.id.select_cun)
    public AutoLinearLayout select_cun;
    @BindView(R.id.iv1)
    public ImageView iv1;
    @BindView(R.id.iv2)
    public ImageView iv2;
    @BindView(R.id.iv3)
    public ImageView iv3;
    @BindView(R.id.iv4)
    public ImageView iv4;
    @BindView(R.id.iv5)
    public ImageView iv5;
    @BindView(edt_address)
    public EditText addressEdt;

    /**
     * 搜索网点
     */
    @OnClick(R.id.shopSS)
    void shopSS() {
        searchShopCode();
    }

    //当前省的名称
    protected String mCurrentProviceName;
   private String apPro,apCity,apDistrict,apTown,apCountry;

    //当前网店编号
    protected String mCurrentShopNum ;
    private String code;
    private String currentLevelId;

    private void  searchShopCode(){
        String num = shopCode.getText().toString();
        mCurrentShopNum = num;
//        if(StringUtils.isEmpty(num.trim())){
//            TipsToast.makeText(AddAddressActivity.this, "请输入搜索编号", TipsToast.LENGTH_SHORT).show();
//            return;
//        }
        OkUtilCallback callback = new OkUtilCallback() {
            @Override
            public void onMyResponse(String response, int id) {
                Gson gson = new Gson();
                ShopNumsBean address = gson.fromJson(response, ShopNumsBean.class);
                if ((!AppConfig.RESULT_OK.equalsIgnoreCase(address.getResult()))) {
                    BaseApplication.getIntstance().showToast(address.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                } else if (AppConfig.RESULT_OK.equalsIgnoreCase(address.getResult())) {
                    if(address.getNums().size()==0){
                        TipsToast.makeText(AddressEdtActivity.this, "暂未搜索到结果，请重新输入", TipsToast.LENGTH_SHORT).show();
                        return;
                    }
                    showPopupShopNum(province, address.getNums());
                }else {
                    TipsToast.makeText(AddressEdtActivity.this, address.getResult(), TipsToast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                TipsToast.makeText(AddressEdtActivity.this, "系统异常,请稍候再试", TipsToast.LENGTH_SHORT).show();
            }
        };

        OKUtilAPI.post("http://admin.erp.jxezt.cn//app/courier/searchByNum.api", "num="+num+"&areaLevel="+currentLevelId, callback);

    }

    @SuppressWarnings("deprecation")
    public void showPopupShopNum(final TextView textView,final List<String> nums) {

        final String[] numArr = new String[nums.size()];
        for(int i=0;i<nums.size();i++){
            numArr[i] = nums.get(i);
        }
        new AlertDialog.Builder(AddressEdtActivity.this)
                .setTitle("请选择")
                .setIcon(android.R.drawable.ic_dialog_info)
                .setSingleChoiceItems(numArr, 0,
                        new DialogInterface.OnClickListener() {

                            public void onClick(final DialogInterface dialog, final int which) {

                                OkUtilCallback callback = new OkUtilCallback() {
                                    @Override
                                    public void onMyResponse(String response, int id) {
                                        Gson gson = new Gson();
                                        CourierBean courier = gson.fromJson(response, CourierBean.class);
                                        if ((!AppConfig.RESULT_OK.equalsIgnoreCase(courier.getResult()))) {
                                            BaseApplication.getIntstance().showToast(courier.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                                        } else if (AppConfig.RESULT_OK.equalsIgnoreCase(courier.getResult())) {
                                            if(courier.getCourier()==null){
                                                TipsToast.makeText(AddressEdtActivity.this, "未搜索到关联信息", TipsToast.LENGTH_SHORT).show();
                                                return;
                                            }

                                            select_zhen.setClickable(false);
                                            select_cun.setClickable(false);
                                            if(courier.getAreaTown()!=null){
                                                zhen.setText(courier.getAreaTown().getName());
                                            }
                                            if(courier.getAreaVillage()!=null){
                                                cun.setText(courier.getAreaVillage().getName());
                                            }
//                                            wddzet.setText(courier.getCourier().getAddress());
                                        }else {
                                            TipsToast.makeText(AddressEdtActivity.this, courier.getResult(), TipsToast.LENGTH_SHORT).show();
                                        }
                                        dialog.dismiss();
                                    }

                                    @Override
                                    public void onError(Call call, Exception e, int id) {
                                        super.onError(call, e, id);
                                        TipsToast.makeText(AddressEdtActivity.this, "系统异常,请稍候再试", TipsToast.LENGTH_SHORT).show();
                                    }
                                };
                                shopCode.setText(numArr[which]+"");
                                //机器码请求地址
                                OKUtilAPI.post("http://admin.erp.jxezt.cn//app/courier/getByNum.api", "num="+numArr[which], callback);


                            }
                        }
                )
                .setNegativeButton("取消", null)
                .show();
    }


    @Override
	public void initView() {
		setTitle(R.string.edit_address);
		setTitleLeftImg(R.mipmap.back_icon);


        code = FileUtil.readFileSdcard(FileUtil.getSDRoot()+ File.separator+"code.txt");
//        code = "12121";
        if(!TDevice.isTablet(AddressEdtActivity.this)){
            shopSSRL.setVisibility(View.VISIBLE);
            addressLL.setVisibility(View.GONE);
            wddzrl.setVisibility(View.VISIBLE);
        }
        if(TDevice.isTablet(AddressEdtActivity.this)&&(!StringUtils.isEmpty(code))) {
            //mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);

            OkUtilCallback callback = new OkUtilCallback() {
                @Override
                public void onMyResponse(String response, int id) {
                    Gson gson = new Gson();
                    ATMAddress address = gson.fromJson(response, ATMAddress.class);
                    if ((!AppConfig.RESULT_OK.equalsIgnoreCase(address.getResult()))) {
                        BaseApplication.getIntstance().showToast(address.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                    } else if (AppConfig.RESULT_OK.equalsIgnoreCase(address.getResult())) {
//                        apPro=accpet.getProvince();
//                        apCity=accpet.getCity();
//                        apDistrict=accpet.getCounty();
//                        apTown = accpet.getCounty();;
//                        apCountry = accpet.getCounty();;
                        select_province.setClickable(false);
                        select_city.setClickable(false);
                        select_district.setClickable(false);
                        select_zhen.setClickable(false);
                        select_cun.setClickable(false);
                        iv1.setVisibility(View.GONE);
                        iv2.setVisibility(View.GONE);
                        iv3.setVisibility(View.GONE);
                        iv4.setVisibility(View.GONE);
                        iv5.setVisibility(View.GONE);

                        if (!StringUtils.isEmpty(address.getAddr().getProvince())) {
                            province.setText(address.getAddr().getProvince().substring(address.getAddr().getProvince().indexOf("/") + 1));
                        }
                        if (!StringUtils.isEmpty(address.getAddr().getCity())) {
                            city.setText(address.getAddr().getCity().substring(address.getAddr().getCity().indexOf("/") + 1));
                        }
                        if (!StringUtils.isEmpty(address.getAddr().getArea())) {
                            district.setText(address.getAddr().getArea().substring(address.getAddr().getArea().indexOf("/") + 1));
                            refId = Integer.parseInt(address.getAddr().getArea().substring(0, address.getAddr().getArea().indexOf("/")));
                        }
                        if (!StringUtils.isEmpty(address.getAddr().getTown())) {
                            zhen.setText(address.getAddr().getTown().substring(address.getAddr().getTown().indexOf("/") + 1));
                        }
                        if (!StringUtils.isEmpty(address.getAddr().getProvince())) {
                            cun.setText(address.getAddr().getVillage().substring(address.getAddr().getVillage().indexOf("/") + 1));
                        }
//                        if(!StringUtils.isEmpty(address.getAddr().getProvince())){
//                            city.setText(address.getAddr().getProvince());
//                        }

//                        district.setText(address.getAddr().getArea());
//                        zhen.setText(address.getAddr().getTown());
//                        cun.setText(address.getAddr().getVillage());
                    } else {
                        TipsToast.makeText(AddressEdtActivity.this, address.getResult(), TipsToast.LENGTH_SHORT).show();
                    }

                }

                @Override
                public void onError(Call call, Exception e, int id) {
                    super.onError(call, e, id);
                    TipsToast.makeText(AddressEdtActivity.this, "系统异常,请稍候再试", TipsToast.LENGTH_SHORT).show();
                }
            };

            //机器码请求地址
            OKUtilAPI.post("http://mall.jxezt.cn//app/addrCode.api", "code=" + code, callback);
        }
	}

	@Override
	public void initData() {
		Addresses.ListBean accpet = (Addresses.ListBean) getIntent().getSerializableExtra("ddm.address");
        currentLevelId = accpet.getCurrentLevelId();
        shopCode.setText(accpet.getDotNo());

        wddzet.setText(accpet.getAddress()+"");
        String getAdd=accpet.getAddress();
        String adds[]=getAdd.split(" ");
        if(adds.length==1){
            apTown="";
            apCountry="";
            addressEdt.setText(adds[0]);
        }else if(adds.length==2){
            apTown=adds[0];
            apCountry="";
            addressEdt.setText(adds[1]);
        }else if(adds.length==3){
            apTown=adds[0];
            apCountry=adds[1];
            addressEdt.setText(adds[2]);
        }
        apPro=accpet.getProvince();
        apCity=accpet.getCity();
        apDistrict=accpet.getCounty();
        refId=accpet.getRefId();

		nameEdt.setText(accpet.getName());
		telEdt.setText(accpet.getPhone());
		province.setText(apPro);
		city.setText(apCity);
		district.setText(apDistrict);
        zhen.setText(accpet.getTown());
        cun.setText(accpet.getVillage());
		cb_select.setChecked(accpet.getIsSelect() == 1 ? true : false);
		acceptId = accpet.getId();
		userId = SPUtils.getStringData(this, SPUtils.USERID);

	}

	@Override
	public int getContentViewId() {
		return R.layout.activity_add_address;
	}

    @Override
    protected String getFragmentTag() {
        return null;
    }


    @OnClick(R.id.save_btn)
	void commit() {
		showWaitDialog(R.string.comiting);
		Conpirm();
	}

	private void Conpirm() {

		String isSelet;
        final String aName=nameEdt.getText().toString();
        final String aPhone=telEdt.getText().toString();
        final String aProvince=province.getText().toString();
        final String aCity=city.getText().toString();
        final String aDistrict=district.getText().toString();
        String aZhen=zhen.getText().toString();
        String aCun=cun.getText().toString();

        String details=addressEdt.getText().toString();
        if (details.toString().contains(" ")) {
            String[] str = details.toString().split(" ");
            String str1 = "";
            for (int i = 0; i < str.length; i++) {
                str1 += str[i];
            }
            details=str1;
        }
        final String aDetails=aZhen+" "+aCun+" "+details;
		if (TextUtils.isEmpty(aName)) {
			Toast.makeText(this, "请输入您的真实姓名", Toast.LENGTH_SHORT).show();
			nameEdt.requestFocus();
            hideWaitDialog();
			return;
		}
		if (TextUtils.isEmpty(aPhone)
				|| telEdt.getText().toString().length() != 11) {
			Toast.makeText(this, "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
			telEdt.requestFocus();
            hideWaitDialog();
			return;
		}
//		if (province.getText().equals("--请选择省份--")) {
//			Toast.makeText(this, "请选择省份", Toast.LENGTH_SHORT).show();
//			province.requestFocus();
//			return;
//		}
        if (aCity.equals("--请选择城市--")) {
            Toast.makeText(this, "请选择城市", Toast.LENGTH_SHORT).show();
            city.requestFocus();
            hideWaitDialog();
            return;
        }
        if (aDistrict.equals("--请选择地区--")) {
            Toast.makeText(this, "请选择地区", Toast.LENGTH_SHORT).show();
            district.requestFocus();
            hideWaitDialog();
            return;
        }
        if (aZhen.equals("--请选择镇--")) {
            Toast.makeText(this, "请选择镇", Toast.LENGTH_SHORT).show();
            zhen.requestFocus();
            hideWaitDialog();
            return;
        }
//        if (aCun.equals("--请选择村--")) {
//            Toast.makeText(this, "请选择村", Toast.LENGTH_SHORT).show();
//            cun.requestFocus();
//            hideWaitDialog();
//            return;
//        }
//		if (TextUtils.isEmpty(addressEdt.getText().toString())) {
//			Toast.makeText(this, "请输入您的详细地址", Toast.LENGTH_SHORT).show();
//			addressEdt.requestFocus();
//            hideWaitDialog();
//			return;
//		}
		if (cb_select.isChecked()) {
			isSelet = "1";
		} else {
			isSelet = "0";
		}
		OkUtilCallback callback = new OkUtilCallback() {
			@Override
			public void onMyResponse(String response, int id) {
                hideWaitDialog();
				Gson gson = new Gson();
				AddAddress result = gson.fromJson(response, AddAddress.class);
				if (result.getResult().equals("OK")) {
					TipsToast.makeText(AddressEdtActivity.this, "编辑地址成功", TipsToast.LENGTH_SHORT).show();
					finish();
				} else {
					TipsToast.makeText(AddressEdtActivity.this, result.getResult(), TipsToast.LENGTH_SHORT).show();
				}
			}

			@Override
			public void onError(Call call, Exception e, int id) {
                hideWaitDialog();
				TipsToast.makeText(AddressEdtActivity.this, R.string.network_error, TipsToast.LENGTH_SHORT).show();
			}
		};
        String address = "";
        if(TDevice.isTablet(AddressEdtActivity.this)){
            address = addressEdt.getText().toString();
        }else{
            address = wddzet.getText().toString();
        }
        String params = "userId=" + userId + "&name=" + aName
                + "&phone=" + aPhone + "&province=" + aProvince
                + "&city=" + aCity + "&county=" + aDistrict
                + "&address=" + address + "&isSelect=" + isSelet + "&id=" + acceptId+"&ugrId="+refId+
                "&town=" + aZhen + "&village=" + aCun+"&dotNo="+shopCode.getText().toString()+"&currentLevelId="+currentLevelId;
        if(TDevice.isTablet(AddressEdtActivity.this)){
            params = params + "&deviceType=ATM";
        }
		OKUtilAPI.post(OKUtilAPI.addAddress, params, callback);

	}

	@OnClick(R.id.select_province)
	void Popup1() {
        mProvinceDatas = new ArrayList<MyAddress.ListBean>();
        mProvinceDatas = getProvince();
	}

    @OnClick(R.id.select_city)
    void Popup2() {
        if (isChoose1) {
            mCitisDatas = new ArrayList<MyAddress.ListBean>();
            mCitisDatas = getCity();
        } else {
            Toast.makeText(getApplicationContext(), "请选择省份", Toast.LENGTH_SHORT).show();
        }
    }

    //选择地区
    @OnClick(R.id.select_district)
    void Popup3() {
        if (isChoose2) {
            mDistrictDatas = new ArrayList<MyAddress.ListBean>();
            mDistrictDatas = getDistrict();
        } else {
            Toast.makeText(getApplicationContext(), "请选择城市", Toast.LENGTH_SHORT).show();
        }
    }


    //选择镇
    @OnClick(R.id.select_zhen)
    void Popup4() {
        if (isChoose3) {
            mTownDatas = new ArrayList<MyAddress.ListBean>();
            mTownDatas = getTown();
        } else {
            Toast.makeText(getApplicationContext(), "请选择地区", Toast.LENGTH_SHORT).show();
        }
    }


    //选择村
    @OnClick(R.id.select_cun)
    void Popup5() {
        if (isChoose4) {
            mCountryDatas = new ArrayList<MyAddress.ListBean>();
            mCountryDatas = getCountry();
        } else {
            Toast.makeText(getApplicationContext(), "请选择镇", Toast.LENGTH_SHORT).show();
        }
    }


    public List<MyAddress.ListBean> getProvince() {
        listBean = new ArrayList<MyAddress.ListBean>();
        if (!NetUtils.isConnected(this)) {
            BaseApplication.showToast("请检查你的网络");
            return listBean;
        } else {
            //mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
            OkUtilCallback callback = new OkUtilCallback() {
                @Override
                public void onMyResponse(String response, int id) {
                    Gson gson = new Gson();
                    MyAddress address = gson.fromJson(response, MyAddress.class);
                    if ((!AppConfig.RESULT_OK.equalsIgnoreCase(address.getResult()))) {
                        BaseApplication.getIntstance().showToast(address.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                    } else if (AppConfig.RESULT_OK.equalsIgnoreCase(address.getResult())) {
                        listBean.addAll(address.getList());
                        showPopupWindow1(province, mProvinceDatas);
                    }
                }
            };
            //请求会员资料
            OKUtilAPI.post(OKUtilAPI.loadArea, "parentId="+parentIdCity, callback);
            return listBean;
        }
    }
    private List<MyAddress.ListBean> getCity() {
        listBean = new ArrayList<MyAddress.ListBean>();
        if (!NetUtils.isConnected(this)) {
            BaseApplication.showToast("请检查你的网络");
            return listBean;
        } else {
            //mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
            OkUtilCallback callback = new OkUtilCallback() {
                @Override
                public void onMyResponse(String response, int id) {
                    Gson gson = new Gson();
                    MyAddress address = gson.fromJson(response, MyAddress.class);
                    if ((!AppConfig.RESULT_OK.equalsIgnoreCase(address.getResult()))) {
                        BaseApplication.getIntstance().showToast(address.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                    } else if (AppConfig.RESULT_OK.equalsIgnoreCase(address.getResult())) {
                        listBean.addAll(address.getList());
                        showPopupWindow2(city, mCitisDatas);
                    }
                }
                @Override
                public void onError(Call call, Exception e, int id) {
                    super.onError(call, e, id);
                    TipsToast.makeText(AddressEdtActivity.this, "系统异常,请稍候再试", TipsToast.LENGTH_SHORT).show();
                }
            };
            //请求会员资料
            OKUtilAPI.post(OKUtilAPI.loadArea, "parentId="+cityId, callback);
            return listBean;
        }
    }
    private List<MyAddress.ListBean> getDistrict() {
        mDistrictDatas = new ArrayList<MyAddress.ListBean>();
        if (!NetUtils.isConnected(this)) {
            BaseApplication.showToast("请检查你的网络");
            return mDistrictDatas;
        } else {
            //mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
            OkUtilCallback callback = new OkUtilCallback() {
                @Override
                public void onMyResponse(String response, int id) {
                    Gson gson = new Gson();
                    MyAddress address = gson.fromJson(response, MyAddress.class);
                    if ((!AppConfig.RESULT_OK.equalsIgnoreCase(address.getResult()))) {
                        BaseApplication.getIntstance().showToast(address.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                    } else if (AppConfig.RESULT_OK.equalsIgnoreCase(address.getResult())) {
                        if(address.getList().size()==0){
                            BaseApplication.getIntstance().showToast("暂无信息", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                            district.setText("");
                            isChoose3 = true;
                        }else{
                            mDistrictDatas.addAll(address.getList());
                            showPopupWindow3(district, mDistrictDatas);
                        }
                    }
                }
                @Override
                public void onError(Call call, Exception e, int id) {
                    super.onError(call, e, id);
                    TipsToast.makeText(AddressEdtActivity.this, "系统异常,请稍候再试", TipsToast.LENGTH_SHORT).show();
                }
            };
            //请求会员资料
            OKUtilAPI.post(OKUtilAPI.loadArea, "parentId="+districtId, callback);
            return mDistrictDatas;
        }
    }

    private List<MyAddress.ListBean> getTown() {
        mTownDatas = new ArrayList<MyAddress.ListBean>();
        if (!NetUtils.isConnected(this)) {
            BaseApplication.showToast("请检查你的网络");
            return mTownDatas;
        } else {
            //mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
            OkUtilCallback callback = new OkUtilCallback() {
                @Override
                public void onMyResponse(String response, int id) {
                    Gson gson = new Gson();
                    MyAddress address = gson.fromJson(response, MyAddress.class);
                    if ((!AppConfig.RESULT_OK.equalsIgnoreCase(address.getResult()))) {
                        BaseApplication.getIntstance().showToast(address.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                    } else if (AppConfig.RESULT_OK.equalsIgnoreCase(address.getResult())) {
                        if(address.getList().size()==0){
                            BaseApplication.getIntstance().showToast("暂无信息", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                            zhen.setText("");
                            isChoose4 = true;
                            countryId=townId;
                        }else{
                            mTownDatas.addAll(address.getList());
                            showPopupWindow4(zhen, mTownDatas);
                        }
                    }
                }
                @Override
                public void onError(Call call, Exception e, int id) {
                    super.onError(call, e, id);
                    TipsToast.makeText(AddressEdtActivity.this, "系统异常,请稍候再试", TipsToast.LENGTH_SHORT).show();
                }
            };
            //请求会员资料
            OKUtilAPI.post(OKUtilAPI.loadArea, "parentId="+townId, callback);
            return mTownDatas;
        }
    }
    private List<MyAddress.ListBean> getCountry() {
        mCountryDatas = new ArrayList<MyAddress.ListBean>();
        if (!NetUtils.isConnected(this)) {
            BaseApplication.showToast("请检查你的网络");
            return mCountryDatas;
        } else {
            //mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
            OkUtilCallback callback = new OkUtilCallback() {
                @Override
                public void onMyResponse(String response, int id) {
                    Gson gson = new Gson();
                    MyAddress address = gson.fromJson(response, MyAddress.class);
                    if ((!AppConfig.RESULT_OK.equalsIgnoreCase(address.getResult()))) {
                        BaseApplication.getIntstance().showToast(address.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                    } else if (AppConfig.RESULT_OK.equalsIgnoreCase(address.getResult())) {
                        if(address.getList().size()==0){
                            BaseApplication.getIntstance().showToast("暂无信息", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                            cun.setText("");
                        }else{
                            mCountryDatas.addAll(address.getList());
                            showPopupWindow5(cun, mCountryDatas);
                        }
                    }
                }
                @Override
                public void onError(Call call, Exception e, int id) {
                    super.onError(call, e, id);
                    TipsToast.makeText(AddressEdtActivity.this, "系统异常,请稍候再试", TipsToast.LENGTH_SHORT).show();
                }
            };
            //请求会员资料
            OKUtilAPI.post(OKUtilAPI.loadArea, "parentId="+countryId, callback);
            return mCountryDatas;
        }
    }



    public void showPopupWindow1(final TextView textView,
                                 final List<MyAddress.ListBean> list1) {

        if (window1 == null) {
            LinearLayout linearLayout = (LinearLayout) LayoutInflater
                    .from(this).inflate(R.layout.type_select_spanner_dropdown,
                            null);

            lv = (ListView) linearLayout.findViewById(R.id.listview);
            adapter1 = new ProviceListAdapter1(this, list1);
            lv.setAdapter(adapter1);
            // 设置弹窗的高和宽
            window1 = new PopupWindow(linearLayout);
            window1.setWidth(textView.getWidth());
            window1.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        window1.setBackgroundDrawable(new BitmapDrawable());
        window1.setFocusable(true);
        window1.showAsDropDown(textView);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                                    int position, long arg3) {
                isChoose1 = true;
                mCurrentProviceName = list1.get(position).getName();
                cityId = list1.get(position).getId();
                Log.e("aaa","cityId="+cityId);
                //mCitisDatas.clear();
                textView.setText(mCurrentProviceName);
                city.setText("--请选择城市--");
                district.setText("--请选择地区--");
                zhen.setText("--请选择镇--");
                cun.setText("--请选择村--");
                isChoose2=false;
                isChoose3=false;
                isChoose4=false;
                isChoose5=false;
                window1.dismiss();
            }
        });
    }
    @SuppressWarnings("deprecation")
    private void showPopupWindow2(final TextView textView,final  List<MyAddress.ListBean> list2) {
        LinearLayout linearLayout = (LinearLayout) LayoutInflater
                .from(this).inflate(R.layout.type_select_spanner_dropdown,
                        null);

        lv = (ListView) linearLayout.findViewById(R.id.listview);
        adapter2 = new ProviceListAdapter1(this, list2);
        lv.setAdapter(adapter2);
        // 设置弹窗的高和宽
        window2 = new PopupWindow(linearLayout);
        window2.setWidth(textView.getWidth());
        window2.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        window2.setBackgroundDrawable(new BitmapDrawable());
//        window1.setOutsideTouchable(true);
        window2.setFocusable(true);
        window2.showAsDropDown(textView);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                                    int position, long arg3) {
                isChoose2 = true;
                mCurrentProviceName = list2.get(position).getName();
                districtId = list2.get(position).getId();
                Log.e("aaa","districtId="+districtId);
                //mCitisDatas.clear();
                textView.setText(mCurrentProviceName);
//                if (isChoose1) {
//                    mCitisDatas = getAddress(parentIdCity);
//                } else {
//                }
                window2.dismiss();
            }
        });
    }
    @SuppressWarnings("deprecation")
    private void showPopupWindow3(final TextView district, final List<MyAddress.ListBean> list3) {
        LinearLayout linearLayout = (LinearLayout) LayoutInflater
                .from(this).inflate(R.layout.type_select_spanner_dropdown,
                        null);

        lv = (ListView) linearLayout.findViewById(R.id.listview);
        adapter3 = new ProviceListAdapter1(this, list3);
        lv.setAdapter(adapter3);
        // 设置弹窗的高和宽
        window3 = new PopupWindow(linearLayout);
        window3.setWidth(district.getWidth());
        window3.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        window3.setBackgroundDrawable(new BitmapDrawable());
//        window1.setOutsideTouchable(true);
        window3.setFocusable(true);
        window3.showAsDropDown(district);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                                    int position, long arg3) {
                isChoose3 = true;
                mCurrentProviceName = list3.get(position).getName();
                townId = list3.get(position).getId();
                refId = list3.get(position).getId();
                Log.e("aaa","townId="+townId);
                //mCitisDatas.clear();
                district.setText(mCurrentProviceName);
                window3.dismiss();

                currentLevelId = list3.get(position).getLevelId();

                searchShopCode();
            }
        });
    }
    @SuppressWarnings("deprecation")
    private void showPopupWindow4(final TextView district, final List<MyAddress.ListBean> list3) {
        LinearLayout linearLayout = (LinearLayout) LayoutInflater
                .from(this).inflate(R.layout.type_select_spanner_dropdown,
                        null);

        lv = (ListView) linearLayout.findViewById(R.id.listview);
        adapter4 = new ProviceListAdapter1(this, list3);
        lv.setAdapter(adapter4);
        // 设置弹窗的高和宽
        window4= new PopupWindow(linearLayout);
        window4.setWidth(district.getWidth());
        window4.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        window4.setBackgroundDrawable(new BitmapDrawable());
//        window1.setOutsideTouchable(true);
        window4.setFocusable(true);
        window4.showAsDropDown(district);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                                    int position, long arg3) {
                isChoose4= true;
                mCurrentProviceName = list3.get(position).getName();
                countryId = list3.get(position).getId();
                refId = list3.get(position).getId();
                Log.e("aaa","countryId="+countryId);
                //mCitisDatas.clear();
                district.setText(mCurrentProviceName);
                window4.dismiss();
            }
        });
    }
    @SuppressWarnings("deprecation")
    private void showPopupWindow5(final TextView district, final List<MyAddress.ListBean> list3) {
        LinearLayout linearLayout = (LinearLayout) LayoutInflater
                .from(this).inflate(R.layout.type_select_spanner_dropdown,
                        null);

        lv = (ListView) linearLayout.findViewById(R.id.listview);
        adapter5 = new ProviceListAdapter1(this, list3);
        lv.setAdapter(adapter5);
        // 设置弹窗的高和宽
        window5 = new PopupWindow(linearLayout);
        window5.setWidth(district.getWidth());
        window5.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        window5.setBackgroundDrawable(new BitmapDrawable());
//        window1.setOutsideTouchable(true);
        window5.setFocusable(true);
        window5.showAsDropDown(district);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                                    int position, long arg3) {
                isChoose5 = true;
                mCurrentProviceName = list3.get(position).getName();
                refId = list3.get(position).getId();
                Log.e("aaa","refId="+refId);
                //mCitisDatas.clear();
                district.setText(mCurrentProviceName);
                window5.dismiss();
            }
        });
    }

}
